***********************************************************************************************************************
********** THIS .DO FILE PERFORMS A DENSITY TEST ON THE INCUMBENT SHARE (to test manipulation by incumbents) **********
***********************************************************************************************************************

clear *
use "${root}/data/processed/final_sample.dta", clear

gen incumbent_rank=.
forvalues i = 1(1)13 {
	replace incumbent_rank=`i' if (rank_`i'_nome_candidato == incumbent_mayor & rank_`i'_nome_candidato!="" & incumbent_mayor!="")	
	}

gen incumbent_margin=.
replace incumbent_margin = rank_1_vote_share-rank_2_vote_share if incumbent_rank==1
forvalues i = 2(1)13 {
	replace incumbent_margin = rank_`i'_vote_share - rank_1_vote_share if incumbent_rank==`i'
	}
	
*consistency check
count if incumbent_margin<0 & incumbent_margin!=. & lame_duck==1
count if incumbent_margin>0 & incumbent_margin!=. & lame_duck==0

keep incumbent_margin *_sample lame_duck
set scheme plotplain

* Baseline sample
keep if baseline_sample == 1

rddensity incumbent_margin, c(0) plot plot_range(-50 50)  ///
graph_opt(xtitle("Incumbent mayor margin") legend(off position(6)) ///
graphr(c(white)))
graph export "${root}/results/figures/incumbent_manipulation_test.pdf", as(pdf) replace


* Tiebout median subsample

rddensity incumbent_margin if tiebout_median_sample == 1, c(0) plot  plot_range(-50 50)   ///
graph_opt(xtitle("Incumbent mayor margin") legend(off position(6)) ///
graphr(c(white)) name(tiebout_median) title("Tiebout<median"))


* Coalition distance subsample

rddensity incumbent_margin if coal_dist_median_sample == 1, c(0) plot  plot_range(-50 50)  ///
graph_opt(xtitle("Incumbent mayor margin") legend(off position(6)) ///
graphr(c(white)) name(coal_dist) title("Ideology distance>median"))

* Oil-windfalls subsample

rddensity incumbent_margin if oil_sample == 1, c(0) plot  plot_range(-50 50)   ///
graph_opt(xtitle("Incumbent mayor margin") legend(off position(6)) ///
graphr(c(white)) name(oil) title("Oil windfall"))

graph combine tiebout_median coal_dist oil, xcommon row(2) cols(2) b1(Left margin of victory)
graph export "${root}/results/figures/incumbent_manipulation_tests_subsamples.pdf", replace
